
layui.define(['table', 'form'], function(exports){
  var $ = layui.$
  ,table = layui.table
  ,form = layui.form;

  //文章管理
  table.render({
    elem: '#LAY-app-content-list'
    ,url: layui.setter.prefixUrl + 'GetActiveCodeList' //模拟接口
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'code', align: 'center',width: 200, title: '授权码', sort: true}
	  
		,{field: 'codeType', align: 'center',width: 250, title: '授权码类型', 
		
		templet: function (d) {
		 
		
		    if(d.codeType==0)
			{
				return "永不过期 "
			}
			else
			{
				return "过期限制 "
			}
		 
		 },
		 
		sort: true}
		
		
	  ,{field: 'note', align: 'center',width: 250, title: '说明', sort: true}
	  
	 
	   ,{field: 'expiretime', align: 'center',width: 250, title: '有效期', 
	   
	   templet: function (d) {
	    
			if(d.codeType==0)
			{
				return "-- "
			}
			else
			{
				 return formatServerDateTime(d.expiretime);
			}
	    
	    
	    },
		
		sort: true}
		
      ,{field: 'createTime', align: 'center',Width: 200,title: '创建时间',
	  templet: function (d) {
	   
	   return formatServerDateTime(d.createTime);
	   
	   },
	  
	  sort: true}
      ,{title: '操作', minWidth: 150, align: 'center', fixed: 'right', toolbar: '#table-content-list'}
    ]]
    ,page: true
    ,limit: 10
    ,limits: [10, 15, 20, 25, 30]
    ,text: '对不起，加载出现异常！'
  });
  
  //监听工具条
  table.on('tool(LAY-app-content-list)', function(obj){
    var data = obj.data;
    if(obj.event === 'del'){
      layer.confirm('确定删除此条数据吗', function(index){
		var url = layui.setter.prefixUrl + "DeleteCode/"+data.id;
		$.post(url, function(data){ 
				 if(data==0)
				 {
					 table.reload('LAY-app-content-list');
					  layer.close(index); 
				 }
		 });
       
      });
    } else if(obj.event === 'edit'){
      layer.open({
        type: 2
        ,title: '编辑授权码'
        ,content: '../../views/code/listform.html?id='+ data.id
        ,maxmin: true
        ,area: ['550px', '550px']
        ,btn: ['确定', '取消']
        ,yes: function(index, layero){
			
          var iframeWindow = window['layui-layer-iframe'+ index]
          ,submit = layero.find('iframe').contents().find("#layuiadmin-app-form-edit");

          //监听提交
          iframeWindow.layui.form.on('submit(layuiadmin-app-form-edit)', function(data){
            var field = data.field; //获取提交的字段
			var _expiretime = field.expiretime;
			 var id = field.id;
			 var codeType = field.codeType;
			 var note = field.note;
			 
			
			if(codeType == 0 || _expiretime=="")
			{
			     _expiretime = 0;
			}
			var url = layui.setter.prefixUrl+"ModifyCode/"+id+ "/"+note + "/"+codeType+"/"+_expiretime;

			$.post(url, field,function(data){ 
						
						 if(data==0)
						 {
							 table.reload('LAY-app-content-list');
							 layer.close(index);
							 layer.alert("修改成功");
						 }
						 else
						 {
							 layer.alert(data.msg);
						 }
					
				 });
            
          });  
          
          submit.trigger('click');
        }
      });
    }
  });
  
  exports('activecode', {})
});